const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

router.post('/menu/getMenuList', async ctx => {
    // 从租户的菜单中取
    try {
        let roleId = ctx.user.roleId
        let tid = ctx.user.tenantId

        if(roleId) {

            let role = await db.models['role'].findOne({
                where: {
                    id: roleId
                }
            })
            let list = []
            let allMenu = ` select b.* from tenant_menu a inner join menu_v5 b on a.menuId=b.id and a.tenantId=${tid} ` // 该租户有的菜单
            let sql = `select  a.id,a.name,a.pid,a.sort,a.routeId,a.icon,a.sep,a.allowAdd,a.allowEdit,a.allowDel, a.isBill,a.isReport,a.isAdmin,b.canAdd,
                                b.canEdit,b.canDel,b.canAccount,b.canWaste, a.isMobile,a.isPc, a.path ,a.type ,a.display ,a.tag,a.addRoute,a.window 
                                   from (${allMenu}) a inner join (select * from role_menu where roleId=${roleId}) b  
                                   on a.id=b.menuId where isnull(a.isDelete,0)=0 and  isnull(a.isAdmin,0)=0 order by a.sort asc`
            let showMenu = []
            if(ctx.user.username === 'admin') {

                let str = `select id,name,pid,sort,routeId,icon,sep,allowAdd,allowEdit,allowDel,isBill,isReport,isAdmin,display,
                                    1 canAdd,1 canEdit,1 canDel,1 canAccount,1 canWaste,bt,params,page, 
                                    isMobile,isPc ,path ,type,tag,addRoute,window from (${allMenu}) a order by sort 
                                    `
                list = await db.query(str, {type:　QueryTypes.SELECT})
                showMenu = list
            }
            else if(role && role.name === '管理员') {
                let sqlstr = `select  id,name,pid,sort,routeId,icon,sep,allowAdd,allowEdit,allowDel,isBill,isReport,isAdmin,bt ,1 canAdd,display,
                1 canEdit,1 canDel,1 canAccount,1 canWaste,params,page, isMobile,isPc ,path ,type,tag,addRoute,window  from (${allMenu}) a where isnull(a.isAdmin,0)=0 order by sort `
                list = await db.query(sqlstr, {type: QueryTypes.SELECT})
                showMenu = list

            }
            else {
                let sqlstr = `select  id,name,pid,sort,routeId,icon,sep,allowAdd,allowEdit,allowDel,isBill,isReport,isAdmin,bt ,1 canAdd,display,
                1 canEdit,1 canDel,1 canAccount,1 canWaste,params,page, isMobile,isPc ,path ,type,tag,addRoute,window  from (${allMenu}) a where isnull(a.isAdmin,0)=0 order by sort `
                list = await db.query(sqlstr, {type: QueryTypes.SELECT})
                showMenu = await db.query(sql, {type: QueryTypes.SELECT})
            }
            let menus=  []
            list.map(row => {
                menus.push({
                    id: row.id,
                    pid: row.pid,
                    path: row.pid? '/main/'+row.routeId:'/main',
                    name: row.routeId,
                    component: row.pid ? `/${row.page}.vue` :'layout/routerView/parent',
                    display: row.display?1:0,
                    tag: row.tag,
                    meta: {
                        routeId: row.routeId,
                        title: row.name,
                        name: row.name,
                        isLink: '',
                        isHide: false,
                        isKeepAlive: true,
                        isAffix: false,
                        isIframe: false,
                        addRoute: row.addRoute
                    },
                })
            })
            let m = tool.toArrayTree(menus)
            m.unshift({
                path: '/home',
                name: 'home',
                component: '/home/index.vue',
                meta: {
                    title: '首页',
                    isLink: '',
                    isHide: false,
                    isKeepAlive: true,
                    isAffix: true,
                    isIframe: false,
                },
            },)
            return ctx.suc('', {
                routeMenu: m,
                showMenu:tool.toArrayTree(showMenu)
            })
        }
        return ctx.suc('', [])
    }
    catch (e) {
        return ctx.err('菜单取回失败')
    }
})


module.exports = router;
